package com.iflytek.uoamp.notify.wechat.format;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.iflytek.uoamp.common.notify.enumerate.State;
import com.iflytek.uoamp.common.notify.obj.Content;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public class FormatBaseWechat extends FixedWechatFormatter {
	
    private static final String BASE = "主机";
    
    public String pretty(Notification notification, List<?> contents) {
        Content content = (Content) contents.get(0);
        StringBuilder builder = new StringBuilder(TEMPLATE_ID + "@@");
        builder.append("{\"monitortime\":\"");
        builder.append(content.getMonitorTime());
        
        builder.append("\",\"monitortype\":\"");
        builder.append(BASE);
        
        builder.append("\",\"monitorname\":\"");
        builder.append(getTitle(contents));
        
        builder.append("\",\"state\":\"");
        builder.append(State.getName((content.getState())));
        
        builder.append("\",\"statedes\":\"");
        
        if(StringUtils.isNotEmpty(NotifyUtils.getAttributeValue(content.getOutput(), "mailstatedes"))){
        	builder.append(NotifyUtils.getAttributeValue(content.getOutput(),"mailstatedes"));
	   	}
	   	else{
	   		if(content.getOutput().contains("subject=") && content.getOutput().contains("monitorname=")){
	   			builder.append(content.getOutput().substring(content.getOutput().indexOf((char)31)+1, content.getOutput().length()).replace("\"", "\\\""));
	   		}
	   		else{
	   			builder.append("");
	   		}
	   	}
        
        builder.append("\"}");
        
        return builder.toString();
    }
    
    public String getTitle(List<?> contents) {
        Content content = (Content)contents.get(0);
        String output = content.getOutput();
        
        return NotifyUtils.getAttributeValue(output, "subject");
    }
    
}
